/**
 * Project: TacticaPescaditos
 * File: GlobalPlayerState.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.modelo.Posicion;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TacticaPescaditos;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TeamPlayer;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics.Vector2D;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: GlobalPlayerState
 */
public class GlobalPlayerState implements State<TeamPlayer> {
	/**
	 * 
	 */
	private GlobalPlayerState() {
		// TODO Auto-generated constructor stub
	}

	public static final State<TeamPlayer> instance = new GlobalPlayerState();

	public void enter(TeamPlayer o) {
		// TODO Auto-generated method stub
		
	}

	public void execute(TeamPlayer o) {
		// TODO Auto-generated method stub
		
	}

	public void exit(TeamPlayer o) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("unchecked")
	public boolean onMessage(TeamPlayer player, Telegram telegram) {

		switch(telegram.getMessageType().ordinal())
		{
		case 0: // Telegram.MessageType.HELP_ME

			if (!player.getStateMachine().isInState(SupportAttacker.instance))   {


				player.getSteering().setTarget(player.getTactica().getSupportSpot());
				player.getStateMachine().changeState(SupportAttacker.instance);
			}
			return true;


		case 1: // Telegram.MessageType.PASS_TO_ME


			TeamPlayer receiver =  (TeamPlayer)telegram.getExtraInfo(); 

			if (player.getTactica().getReceiver() == null &&
					player.canKick() )
			{
				Vector2D recPos = receiver.getPosition();
				
				player.kick(new Posicion(recPos.x,recPos.y),1,TacticaPescaditos.PASS_ANGLE);
				
				player.getTactica().getDispatcher().dispatch(player.getId(), receiver.getId(), 
						Telegram.MessageType.RECEIVE_BALL, receiver.getPosition()
						,0);

				player.getStateMachine().changeState(Wait.instance);

				player.findSupport();
			}

			return true;



		case 2: // Telegram.MessageType.RECEIVE_BALL

			player.getSteering().setTarget((Vector2D)telegram.getExtraInfo());
			player.getStateMachine().changeState(ReceiveBall.instance);
			return true;




		case 3: // Telegram.MessageType.WAIT

			player.getStateMachine().changeState(Wait.instance);

			return true;
			
		case 4: // Telegram.MessageType.CHANGE_STATE
			player.getStateMachine().changeState((State<TeamPlayer>)telegram.getExtraInfo());
			return true;



		}
		return false;
	}

}
